<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class ResourceController extends BaseController {

	
    public function type(){

    	$resource = D(C('TABLE_RESOURCE'));
		$userid = session('userinfo')['id'];
		if($userid==37){
			$res = $resource->where(array('userid'=>37))->order(array('id'=>'desc'))->select();
		}else if($userid==39){
			$res = $resource->where(array('userid'=>39))->order(array('id'=>'desc'))->select();
		}else{
			$res = $resource->order(array('id'=>'desc'))->select();
		}
    	$this->assign('data',$res);
    	$this->assign('data1',$res);
    	$this->display();
    }
    
    public function resourceadd(){
    	$this->assign('resourcelist',$this->getresourtype());
    	$this->assign('resourcelist1',$this->getresourtype());
    	$this->display();
    }
    
    public function editsubresource(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$resource = D(C('TABLE_RESOURCE'));
    		$res = $resource->find($id);
    		if($res){
    			$this->assign('resourcearticle',$res);
    			$this->assign('resourcelist',$this->getresourtype());
    			$this->assign('resourcelist1',$this->getresourtype());
    			$this->display();
    		}else{
    			$this->error('无数据可用',1);
    		}
    		
    	}else{
    		$this->error('参数错误',1);
    	}
    }
    
    /*
     * 删除
     */
    public function deletesubresource(){
    	$id = I('get.id');
    	if(!empty($id)){
    		$resource = D(C('TABLE_RESOURCE'));
    		$res = $resource->delete($id);
    		if($res){
    			$this->success('删除成功！');
    		}
    	}else{
    		$this->error('参数错误',1);
    	}
    }
    
    /*
     * 列出资源分类
     */
    public function getresourtype(){
		$userid = session('userinfo')['id'];
    	$resource = D(C('TABLE_RESOURCE'));
		if(session('userinfo')['id']==37){
			$res = $resource->where(array('userid'=>37))->select();
		}else if(session('userinfo')['id']==39){
			$res = $resource->where(array('userid'=>39))->select();
		}else{
			$res = $resource->order('id desc')->select();
		}
    	return $res;
    }
    
    /*
     * 添加资源栏目
     */
    public function resourcedataadd(){
    	$rules = array(
    			array('typename','require','分类名称不能为空！'),
    	);
    	$resource = D(C('TABLE_RESOURCE'));
    	if(!$resource->validate($rules)->create()){
    		$this->error($resource->getError(),'',1);
    	}else{
			$resource->typedate = date("Y-m-d H:i:m");
			$resource->userid = session('userinfo')['id'];
			$result = $resource->add();
			if($result){
				$this->success('添加成功','/Admin/Resource/type');
			}else{
				$this->error('添加失败','',1);
			}
    	}
    }
    
    /*
     * 修改资源栏目
     */
    public function resourcedataedit(){
    	$id = I('get.id');
    	if($id == I('post.pid')){
    		$this->error('所属分类不能选择自己','',5);
    		exit;
    	}
    	$rules = array(
    			array('typename','require','分类名称不能为空！'),
    	);
    	$resource = D(C('TABLE_RESOURCE'));
    	if(!$resource->validate($rules)->create()){
    		$this->error($resource->getError(),'',1);
    	}else{
    		$result = $resource->where(array('id'=>$id))->save();
    		if($result){
    			$this->success('修改成功','/Admin/Resource/type');
    		}else{
    			$this->error('修改失败','',1);
    		}
    	}
    }

	/*
	 * 读取某个分类的广告图
	 */
	public function resourcebanner(){
		$resid = I('get.resid');
		$resource = D(C('TABLE_RESOUVRCEBANNER'));
		$res = $resource->where(array('resid'=>$resid))->order('bannersort asc')->select();
		$this->assign('bannerlist',$res);
		$this->display('resourcebanner');
	}

	/*
	 * 添加广告图
	 */
	public function resourcebanneradd(){
		$rules = array(
			array('bannerimg','require','广告图不能为空！'),
		);
		$resource = D(C('TABLE_RESOUVRCEBANNER'));
		if(!$resource->validate($rules)->create()){
			$this->error($resource->getError(),'',1);
		}else{
			$resid = I('get.resourceid');
			$resource->resid = $resid;
			$resource->bannertime = date("Y-m-d H:i:m");
			$res = $resource->add();
			if($res){
				$this->success('添加成功');
			}else{
				$this->error('添加失败','',1);
			}
		}

	}

	/*
	 * 删除广告图
	 */
	public function resourcedelete(){
		$resid = I('get.id');
		$resource = D(C('TABLE_RESOUVRCEBANNER'));
		$res = $resource->where(array('id'=>$resid))->delete();
		if($res){
			$this->success('删除成功');
		}else{
			$this->error('删除失败','',1);
		}
	}
  
    
}